package com.rich.quartz.controller;

import com.rich.quartz.entity.ScheduleJob;
import com.rich.quartz.quartzjobs.QuartzJob;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author HanSama
 * @date 2021年08月17日 15:08
 */
@RestController
@RequestMapping("/quartz")
public class QuartzController {

    @Autowired
    private Scheduler scheduler;



    /**
     * 任务创建与更新(未存在的就创建，已存在的则更新)
     */
    @RequestMapping(value="/startQuartz")
    @ResponseBody
    public Map updateQuartz(String jobName, String jobGroup,String cron){
        Map map = new HashMap<>(64);
        ScheduleJob job = new ScheduleJob();
        job.setJobGroup(jobGroup.trim());
        job.setJobName(jobName.trim());
        System.out.println(jobName+","+jobGroup);
        try {
            //获取触发器标识
            TriggerKey triggerKey = TriggerKey.triggerKey(jobName.trim(), jobGroup.trim());
            //获取触发器trigger
            CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
            if(null==trigger){//不存在任务
                //创建任务
                JobDetail jobDetail = JobBuilder.newJob(QuartzJob.class)
                        .withIdentity(jobName.trim(), jobGroup.trim())
                        .usingJobData("page",1)
                        .build();
                jobDetail.getJobDataMap().put("scheduleJob", job);
                //表达式调度构建器
                //这里的时间也可以通过页面传送过来。具体怎么写请看上一篇quartz介绍
                CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cron);
                //按新的cronExpression表达式构建一个新的trigger
                trigger = TriggerBuilder.newTrigger()
                        .withIdentity(job.getJobName(), job.getJobGroup())
                        .withSchedule(scheduleBuilder)
                        .build();
                scheduler.scheduleJob(jobDetail, trigger);
                map.put("msg", "创建新任务");
                map.put("code", 200);
            }else{//存在任务
                // Trigger已存在，那么更新相应的定时设置
                //表达式调度构建器
                CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cron);
                //按新的cronExpression表达式重新构建trigger
                trigger = trigger.getTriggerBuilder()
                        .withIdentity(triggerKey)
                        .withSchedule(scheduleBuilder)
                        .build();
                //按新的trigger重新设置job执行
                scheduler.rescheduleJob(triggerKey, trigger);
                map.put("msg", "启动任务成功");
                map.put("code", 200);
            }
        } catch (SchedulerException e) {
            e.printStackTrace();
            map.put("msg", "启动失败");
            map.put("code", 500);
        }
        return map;
    }

    /**
     * 删除任务
     */
    @RequestMapping(value="/removeQuartz")
    public String deleteQuartz(String jobName,String jobGroup){
        JobKey jobKey = JobKey.jobKey(jobName.trim(), jobGroup.trim());
        System.out.println("删除"+jobName.trim()+"任务");
        try {
            scheduler.deleteJob(jobKey);
            System.out.println("删除任务成功");
            return "删除任务成功";
        } catch (SchedulerException e) {
            e.printStackTrace();
            System.out.println("删除任务成功");
            return "删除任务成功";
        }
    }

    /**
     * 暂停任务
     */
    @RequestMapping(value="/pauseQuartz")
    public String pauseQuartz(String jobName,String jobGroup){
        ScheduleJob job = new ScheduleJob();
        job.setJobGroup(jobGroup);
        job.setJobName(jobName);
        System.out.println(jobName+","+jobGroup);
        JobKey jobKey = JobKey.jobKey(jobName, jobGroup);
        try {
            scheduler.pauseJob(jobKey);
            System.out.println("任务成功暂停");
            return "任务成功暂停";
        } catch (SchedulerException e) {
            e.printStackTrace();
            System.out.println("任务暂停失败");
            return "任务暂停失败";
        }
    }


    /**
     * 恢复任务
     */
    @RequestMapping(value="/resumeQuartz")
    public String resumeQuartz(String jobName,String jobGroup){
        ScheduleJob job = new ScheduleJob();
        job.setJobGroup(jobGroup);
        job.setJobName(jobName);
        System.out.println(jobName+","+jobGroup);
        JobKey jobKey = JobKey.jobKey(jobName, jobGroup);
        try {
            scheduler.resumeJob(jobKey);
            System.out.println("任务成功恢复");
            return "任务成功恢复";
        } catch (SchedulerException e) {
            e.printStackTrace();
            System.out.println("任务恢复失败");
            return "任务恢复失败";
        }
    }

}
